#pragma once
#include <odb/core.hxx>
#include <odb/nullable.hxx>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <string>

#pragma db object
class Message {
public:
    Message() = default;
    Message(const std::string& message_id, const std::string& session_id, const std::string& user_id,
            unsigned char type, const boost::posix_time::ptime& create_time)
        : _message_id(message_id),
          _session_id(session_id),
          _user_id(user_id),
          _type(type),
          _create_time(create_time) {}

    std::string message_id() const { return _message_id; }
    std::string session_id() const { return _session_id; }
    std::string user_id() const { return _user_id; }
    unsigned char type() const { return _type; }
    boost::posix_time::ptime create_time() const { return _create_time; }
    std::string content() const { return !_content ? "" : *_content; }
    std::string file_id() const { return !_file_id ? "" : *_file_id; }
    std::string file_name() const { return !_file_name ? "" : *_file_name; }
    int file_size() const { return !_file_size ? 0 : *_file_size; }


    void set_content(const std::string& content) { _content = content; }
    void set_file_id(const std::string& file_id) { _file_id = file_id; }
    void set_file_name(const std::string& file_name) { _file_name = file_name; }
    void set_file_size(int file_size) { _file_size = file_size; }
    void set_message_id(const std::string& message_id) { _message_id = message_id; }
    void set_session_id(const std::string& session_id) { _session_id = session_id; }
    void set_user_id(const std::string& user_id) { _user_id = user_id; }
    void set_type(unsigned char type) { _type = type; }
    void set_create_time(const boost::posix_time::ptime& create_time) { _create_time = create_time; }
private:
    friend class odb::access;

    #pragma db id auto
    unsigned long _id; // 数据库分配的 ID

    #pragma db unique index type("varchar(64)")
    std::string _message_id; // 消息 ID

    #pragma db index type("varchar(64)")
    std::string _session_id; // 聊天会话 ID

    #pragma db type("varchar(64)")
    std::string _user_id; // 发送者 ID

    #pragma db type("TINYINT UNSIGNED")
    unsigned char _type; // 0-文本消息 1-图片消息 2-文件消息 3-语音消息

    #pragma db type("timestamp")
    boost::posix_time::ptime _create_time; // 消息创建时间

    #pragma db type("TEXT")
    odb::nullable<std::string> _content; // 消息内容
    
    #pragma db type("varchar(64)")
    odb::nullable<std::string> _file_id; 

    #pragma db type("varchar(64)")
    odb::nullable<std::string> _file_name; // 文件名

    #pragma db type("BIGINT UNSIGNED")
    odb::nullable<int> _file_size;
};